home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / ck5a189s / ckucmd.c < prev    next >
C/C++ Source or Header  |  1993-06-04  |  70KB  |  2,230 lines

  1. int cmdmsk = 127; /* Command-terminal-to-C-Kermit character mask */
  2.  
  3. #include "ckcdeb.h"                     /* Formats for debug(), etc. */
  4. _PROTOTYP( int unhex, (char) );
  5.  
  6. #ifndef NOICP     /* The rest only if interactive command parsing selected */
  7.  
  8. char *cmdv = "Command package 5A(053), 21 Nov 92";
  9.  
  10. /*  C K U C M D  --  Interactive command package for Unix  */
  11.  
  12. /*
  13.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  14.   Columbia University Academic Information Systems, New York City.
  15.  
  16.   Copyright (C) 1985, 1993, Trustees of Columbia University in the City of New
  17.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  18.   sold for profit as a software product itself, nor may it be included in or
  19.   distributed with commercial products or otherwise distributed by commercial
  20.   concerns to their clients or customers without written permission of the
  21.   Office of Kermit Development and Distribution, Columbia University.  This
  22.   copyright notice must not be removed, altered, or obscured.
  23. */
  24.  
  25. /*
  26. Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP.
  27. Features:
  28. . parses and verifies keywords, filenames, text strings, numbers, other data
  29. . displays appropriate menu or help message when user types "?"
  30. . does keyword and filename completion when user types ESC or TAB
  31. . does partial filename completion
  32. . accepts any unique abbreviation for a keyword
  33. . allows keywords to have attributes, like "invisible" and "abbreviation"
  34. . can supply defaults for fields omitted by user
  35. . provides command line editing (character, word, and line deletion)
  36. . accepts input from keyboard, command files, or redirected stdin
  37. . allows for full or half duplex operation, character or line input
  38. . settable prompt, protected from deletion
  39.  
  40. Functions:
  41.  cmsetp - Set prompt (cmprom is prompt string)
  42.  cmsavp - Save current prompt
  43.  prompt - Issue prompt
  44.  cmini - Clear the command buffer (before parsing a new command)
  45.  cmres - Reset command buffer pointers (before reparsing)
  46.  cmkey - Parse a keyword
  47.  cmnum - Parse a number
  48.  cmifi - Parse an input file name
  49.  cmofi - Parse an output file name
  50.  cmdir - Parse a directory name (UNIX only)
  51.  cmfld - Parse an arbitrary field
  52.  cmtxt - Parse a text string
  53.  cmcfm - Parse command confirmation (end of line)
  54.  
  55. Return codes:
  56.  -3: no input provided when required
  57.  -2: input was invalid (e.g. not a number when a number was required)
  58.  -1: reparse required (user deleted into a preceding field)
  59.   0 or greater: success
  60. See individual functions for greater detail.
  61.  
  62. Before using these routines, the caller should #include ckucmd.h, and set the
  63. program's prompt by calling cmsetp().  If the file parsing functions cmifi,
  64. cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
  65. system support module for the appropriate system, e.g. ckufio for Unix.  If
  66. the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
  67. then these functions will provide line editing -- character, word, and line
  68. deletion, as well as keyword and filename completion upon ESC and help
  69. strings, keyword, or file menus upon '?'.  If the caller puts the terminal
  70. into character wakeup/noecho mode, care should be taken to restore it before
  71. exit from or interruption of the program.  If the character wakeup mode is not
  72. set, the system's own line editor may be used.
  73.  
  74. NOTE: Contrary to expectations, many #ifdef's have been added to this module.
  75. Any operation requiring an #ifdef (like clear screen, get character from
  76. keyboard, erase character from screen, etc) should eventually be turned into a
  77. call to a function that is defined in ck?tio.c, but then all the ck?tio.c
  78. modules would have to be changed...
  79. */
  80.  
  81. /* Includes */
  82.  
  83. #include "ckcker.h"            /* (===OS2 addition===) */
  84. #include "ckcasc.h"            /* ASCII character symbols */
  85. #include "ckucmd.h"                     /* Command parsing definitions */
  86. #include <errno.h>            /* Error number symbols */
  87.  
  88. #ifdef OSK
  89. #define cc ccount            /* OS-9/68K compiler bug */
  90. #endif /* OSK */
  91.  
  92. #ifdef GEMDOS                /* Atari ST */
  93. #ifdef putchar
  94. #undef putchar
  95. #endif /* putchar */
  96. #define putchar(x) conoc(x)        /* Why doesn't everyone do this? */
  97. #endif /* GEMDOS */
  98.  
  99. /* Local variables */
  100.  
  101. static
  102. int psetf = 0,                          /* Flag that prompt has been set */
  103.     cc = 0,                             /* Character count */
  104.     dpx = 0,                            /* Duplex (0 = full) */
  105.     inword = 0;                /* In the middle of getting a word */
  106.  
  107. static
  108. int hw = HLPLW,                         /* Help line width */
  109.     hc = HLPCW,                         /* Help line column width */
  110.     hh,                                 /* Current help column number */
  111.     hx;                                 /* Current help line position */
  112.  
  113. #define PROML 160                       /* Maximum length for prompt */
  114.  
  115. char *dfprom = "Command? ";             /* Default prompt */
  116.  
  117. int cmflgs;                             /* Command flags */
  118. int cmfsav;                /* A saved version of them */
  119.  
  120. #ifdef DCMDBUF
  121. char *cmdbuf;                /* Command buffer */                
  122. char *savbuf;                /* Help string buffer */            
  123. char *atmbuf;                /* File name buffer */              
  124. char *atxbuf;                /* For expanding the atom buffer */ 
  125. static char *hlpbuf;            /* Atom buffer */                   
  126. static char *atybuf;            /* For copying atom buffer */       
  127. static char *filbuf;            /* Buffer to save copy of command */
  128. static char *cmprom;            /* Program's prompt */
  129. static char *cmprxx;            /* Program's prompt, unevaluated */
  130. #else
  131. char cmdbuf[CMDBL+4];                   /* Command buffer */                
  132. char savbuf[CMDBL+4];                   /* Buffer to save copy of command */
  133. char atmbuf[ATMBL+4];                   /* Atom buffer */                   
  134. char atxbuf[CMDBL+4];                   /* For expanding the atom buffer */ 
  135. static char hlpbuf[HLPBL+4];        /* Help string buffer */            
  136. static char atybuf[ATMBL+4];        /* For copying atom buffer */       
  137. static char filbuf[ATMBL+4];        /* File name buffer */              
  138. static char cmprom[PROML+1];        /* Program's prompt */
  139. static char cmprxx[PROML+1];        /* Program's prompt, unevaluated */
  140. #endif /* DCMDBUF */
  141.  
  142.  
  143.  
  144. /* Command buffer pointers */
  145.  
  146. static char *bp,                        /* Current command buffer position */
  147.     *pp,                                /* Start of current field */
  148.     *np;                                /* Start of next field */
  149.  
  150. static int ungw,            /* For ungetting words */
  151.     atxn;                /* Expansion buffer (atxbuf) length */
  152.  
  153. _PROTOTYP( static VOID addhlp, (char *) );
  154. _PROTOTYP( static VOID clrhlp, (void) );
  155. _PROTOTYP( static VOID dmphlp, (void) );
  156. _PROTOTYP( static int gtword, (void) );
  157. _PROTOTYP( static int addbuf, (char *) );
  158. _PROTOTYP( static int setatm, (char *) );
  159. _PROTOTYP( static int cmdgetc, (void) );
  160. _PROTOTYP( static VOID cmdnewl, (char) );
  161. _PROTOTYP( static VOID cmdchardel, (void) );
  162. _PROTOTYP( static VOID cmdecho, (char, int) );
  163. _PROTOTYP( static int test, (int, int) );
  164. #ifdef GEMDOS
  165. _PROTOTYP( extern char *strchr, (char *, int) );
  166. #endif /* GEMDOS */
  167.  
  168. /*  T E S T  --  Bit test  */
  169.  
  170. static int
  171. test(x,m) int x, m; { /*  Returns 1 if any bits from m are on in x, else 0  */
  172.     return((x & m) ? 1 : 0);
  173. }
  174.  
  175. /*  C M S E T U P  --  Set up command buffers  */
  176.  
  177. #ifdef DCMDBUF
  178. int
  179. cmsetup() {
  180.     if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
  181.     if (!(savbuf = malloc(CMDBL + 4))) return(-1);
  182.     savbuf[0] = '\0';
  183.     if (!(hlpbuf = malloc(HLPBL + 4))) return(-1);
  184.     if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
  185.     if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
  186.     if (!(atybuf = malloc(ATMBL + 4))) return(-1);
  187.     if (!(filbuf = malloc(ATMBL + 4))) return(-1);
  188.     if (!(cmprom = malloc(PROML + 4))) return(-1);
  189.     if (!(cmprxx = malloc(PROML + 4))) return(-1);
  190.     return(0);
  191. }
  192. #endif /* DCMDBUF */ 
  193.  
  194. /*  C M S E T P  --  Set the program prompt.  */
  195.  
  196. VOID
  197. cmsetp(s) char *s; {
  198.     strncpy(cmprxx,s,PROML - 1);
  199.     cmprxx[PROML] = NUL;
  200.     psetf = 1;                          /* Flag that prompt has been set. */
  201. }
  202. /*  C M S A V P  --  Save a copy of the current prompt.  */
  203.  
  204. VOID
  205. #ifdef CK_ANSIC
  206. cmsavp(char s[], int n)
  207. #else
  208. cmsavp(s,n) char s[]; int n;
  209. #endif /* CK_ANSIC */
  210. /* cmsavp */ {
  211.     strncpy(s,cmprxx,n-1);
  212.     s[n-1] = NUL;
  213. }
  214.  
  215. /*  P R O M P T  --  Issue the program prompt.  */
  216.  
  217. VOID
  218. prompt(f) xx_strp f; {
  219.     char *sx, *sy; int n;
  220.  
  221.     if (psetf == 0) cmsetp(dfprom);     /* If no prompt set, set default. */
  222.  
  223.     sx = cmprxx;            /* Unevaluated copy */
  224.     if (f) {                /* If conversion function given */
  225.     sy = cmprom;            /* Evaluate it */
  226.     n = PROML;
  227.     if ((*f)(sx,&sy,&n) < 0)    /* If evaluation failed */
  228.       sx = cmprxx;            /* revert to unevaluated copy */
  229.     else
  230.       sx = cmprom;
  231.     }
  232. #ifdef OSK
  233.     fputs(sx, stdout);
  234. #else
  235. #ifdef MAC
  236.     printf("%s", sx);
  237. #else
  238.     printf("\r%s",sx);            /* Print the prompt. */
  239.     fflush(stdout);            /* Now! */
  240. #endif /* MAC */
  241. #endif /* OSK */
  242. }
  243.  
  244. #ifndef NOSPL
  245. VOID
  246. pushcmd() {                /* For use with IF command. */
  247.     strncpy(savbuf,np,CMDBL);        /* Save the dependent clause,  */
  248.     cmres();                /* and clear the command buffer. */
  249.     debug(F110, "pushcmd: savbuf:", savbuf, 0);
  250. }
  251. #endif /* NOSPL */
  252.  
  253. #ifdef COMMENT
  254. /* no longer used... */
  255. VOID
  256. popcmd() {
  257.     strncpy(cmdbuf,savbuf,CMDBL);    /* Put back the saved material */
  258.     *savbuf = '\0';            /* and clear the save buffer */
  259.     cmres();
  260. }
  261. #endif /* COMMENT */
  262.  
  263. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  264.  
  265. VOID
  266. cmres() {  
  267.     inword = cc = 0;            /* Reset character counter. */
  268.     pp = np = bp = cmdbuf;              /* Point to command buffer. */
  269.     cmflgs = -5;                        /* Parse not yet started. */
  270.     ungw = 0;                /* Don't need to unget a word. */
  271. }
  272.  
  273. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  274.  
  275. /*
  276. The argument specifies who is to echo the user's typein --
  277.   1 means the cmd package echoes
  278.   0 somebody else (system, front end, terminal) echoes
  279. */
  280. VOID
  281. cmini(d) int d; {
  282.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  283.     *atmbuf = *savbuf = *atxbuf = *atybuf = *hlpbuf = *filbuf = NUL;
  284.     dpx = d;
  285.     cmres();
  286. }
  287.  
  288. #ifndef NOSPL
  289. /* The following bits are to allow the command package to call itself */
  290. /* in the middle of a parse.  To do this, begin by calling cmpush, and */
  291. /* end by calling cmpop. */
  292.  
  293. #ifdef DCMDBUF
  294. struct cmp {
  295.     int i[5];                /* stack for integers */
  296.     char *c[3];                /* stack for pointers */
  297.     char *b[8];                /* stack for buffer contents */
  298. };
  299. struct cmp *cmp = 0;
  300. #else
  301. int cmp_i[CMDDEP+1][5];            /* Stack for integers */
  302. char *cmp_c[CMDDEP+1][5];        /* for misc pointers */
  303. char *cmp_b[CMDDEP+1][7];        /* for buffer contents pointers */
  304. #endif /* DCMDBUF */
  305.  
  306. int cmddep = -1;            /* Current stack depth */
  307.  
  308. int
  309. cmpush() {                /* Save the command environment */
  310.     char *cp;                /* Character pointer */
  311.  
  312.     if (cmddep >= CMDDEP)        /* Enter a new command depth */
  313.       return(-1);
  314.     cmddep++;
  315.     debug(F101,"&cmpush","",cmddep);
  316.  
  317. #ifdef DCMDBUF
  318.     /* allocate memory for cmp if not already done */
  319.     if (!cmp && !(cmp = (struct cmp *) malloc(sizeof(struct cmp)*(CMDDEP+1))))
  320.       fatal("cmpush: no memory for cmp");
  321.     cmp[cmddep].i[0] = cmflgs;        /* First do the global ints */
  322.     cmp[cmddep].i[1] = cmfsav;
  323.     cmp[cmddep].i[2] = atxn;
  324.     cmp[cmddep].i[3] = ungw;
  325.  
  326.     cmp[cmddep].c[0] = bp;        /* Then the global pointers */
  327.     cmp[cmddep].c[1] = pp;
  328.     cmp[cmddep].c[2] = np; 
  329. #else
  330.     cmp_i[cmddep][0] = cmflgs;        /* First do the global ints */
  331.     cmp_i[cmddep][1] = cmfsav;
  332.     cmp_i[cmddep][2] = atxn;
  333.     cmp_i[cmddep][3] = ungw;
  334.  
  335.     cmp_c[cmddep][0] = bp;        /* Then the global pointers */
  336.     cmp_c[cmddep][1] = pp;
  337.     cmp_c[cmddep][2] = np; 
  338. #endif /* DCMDBUF */
  339.  
  340.     /* Now the buffers themselves.  A lot of repititious code... */
  341.  
  342. #ifdef DCMDBUF
  343.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  344.     if (cp) strcpy(cp,cmdbuf);
  345.     cmp[cmddep].b[0] = cp;
  346.     if (cp == NULL) return(-1);
  347.  
  348.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  349.     if (cp) strcpy(cp,savbuf);
  350.     cmp[cmddep].b[1] = cp;
  351.     if (cp == NULL) return(-1);
  352.  
  353.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  354.     if (cp) strcpy(cp,hlpbuf);
  355.     cmp[cmddep].b[2] = cp;
  356.     if (cp == NULL) return(-1);
  357.  
  358.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  359.     if (cp) strcpy(cp,atmbuf);
  360.     cmp[cmddep].b[3] = cp;
  361.     if (cp == NULL) return(-1);
  362.  
  363.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  364.     if (cp) strcpy(cp,atxbuf);
  365.     cmp[cmddep].b[4] = cp;
  366.     if (cp == NULL) return(-1);
  367.  
  368.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  369.     if (cp) strcpy(cp,atybuf);
  370.     cmp[cmddep].b[5] = cp;
  371.     if (cp == NULL) return(-1);
  372.  
  373.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  374.     if (cp) strcpy(cp,filbuf);
  375.     cmp[cmddep].b[6] = cp;
  376.     if (cp == NULL) return(-1);
  377. #else
  378.     cp = malloc((int)strlen(cmdbuf)+1);    /* 0: Command buffer */
  379.     if (cp) strcpy(cp,cmdbuf);
  380.     cmp_b[cmddep][0] = cp;
  381.     if (cp == NULL) return(-1);
  382.  
  383.     cp = malloc((int)strlen(savbuf)+1);    /* 1: Save buffer */
  384.     if (cp) strcpy(cp,savbuf);
  385.     cmp_b[cmddep][1] = cp;
  386.     if (cp == NULL) return(-1);
  387.  
  388.     cp = malloc((int)strlen(hlpbuf)+1);    /* 2: Help string buffer */
  389.     if (cp) strcpy(cp,hlpbuf);
  390.     cmp_b[cmddep][2] = cp;
  391.     if (cp == NULL) return(-1);
  392.  
  393.     cp = malloc((int)strlen(atmbuf)+1);    /* 3: Atom buffer */
  394.     if (cp) strcpy(cp,atmbuf);
  395.     cmp_b[cmddep][3] = cp;
  396.     if (cp == NULL) return(-1);
  397.  
  398.     cp = malloc((int)strlen(atxbuf)+1);    /* 4: Expansion buffer */
  399.     if (cp) strcpy(cp,atxbuf);
  400.     cmp_b[cmddep][4] = cp;
  401.     if (cp == NULL) return(-1);
  402.  
  403.     cp = malloc((int)strlen(atybuf)+1);    /* 5: Atom buffer copy */
  404.     if (cp) strcpy(cp,atybuf);
  405.     cmp_b[cmddep][5] = cp;
  406.     if (cp == NULL) return(-1);
  407.  
  408.     cp = malloc((int)strlen(filbuf)+1);    /* 6: File name buffer */
  409.     if (cp) strcpy(cp,filbuf);
  410.     cmp_b[cmddep][6] = cp;
  411.     if (cp == NULL) return(-1);
  412. #endif /* DCMDBUF */
  413.  
  414.     cmini(dpx);                /* Initize the command parser */
  415.     return(0);
  416. }
  417.  
  418. int
  419. cmpop() {                /* Restore the command environment */
  420.     debug(F101,"&cmpop","",cmddep);
  421.     if (cmddep < 0) return(-1);        /* Don't pop too much! */
  422.  
  423. #ifdef DCMDBUF
  424.     cmflgs = cmp[cmddep].i[0];        /* First do the global ints */
  425.     cmfsav = cmp[cmddep].i[1];
  426.     atxn = cmp[cmddep].i[2];
  427.     ungw = cmp[cmddep].i[3];
  428.  
  429.     bp = cmp[cmddep].c[0];        /* Then the global pointers */
  430.     pp = cmp[cmddep].c[1];
  431.     np = cmp[cmddep].c[2]; 
  432. #else
  433.     cmflgs = cmp_i[cmddep][0];        /* First do the global ints */
  434.     cmfsav = cmp_i[cmddep][1];
  435.     atxn = cmp_i[cmddep][2];
  436.     ungw = cmp_i[cmddep][3];
  437.  
  438.     bp = cmp_c[cmddep][0];        /* Then the global pointers */
  439.     pp = cmp_c[cmddep][1];
  440.     np = cmp_c[cmddep][2]; 
  441. #endif /* DCMDBUF */
  442.  
  443.     /* Now the buffers themselves. */
  444.  
  445. #ifdef DCMDBUF
  446.     if (cmp[cmddep].b[0]) {
  447.     strncpy(cmdbuf,cmp[cmddep].b[0],CMDBL); /* 0: Command buffer */
  448.     free(cmp[cmddep].b[0]);
  449.     cmp[cmddep].b[0] = NULL;
  450.     }
  451.     if (cmp[cmddep].b[1]) {
  452.     strncpy(savbuf,cmp[cmddep].b[1],CMDBL); /* 1: Save buffer */
  453.     free(cmp[cmddep].b[1]);
  454.     cmp[cmddep].b[1] = NULL;
  455.     }
  456.     if (cmp[cmddep].b[2]) {
  457.     strncpy(hlpbuf,cmp[cmddep].b[2],HLPBL); /* 2: Help buffer */
  458.     free(cmp[cmddep].b[2]);
  459.     cmp[cmddep].b[2] = NULL;
  460.     }
  461.     if (cmp[cmddep].b[3]) {
  462.     strncpy(atmbuf,cmp[cmddep].b[3],ATMBL); /* 3: Atomic buffer! */
  463.     free(cmp[cmddep].b[3]);
  464.     cmp[cmddep].b[3] = NULL;
  465.     }
  466.     if (cmp[cmddep].b[4]) {
  467.     strncpy(atxbuf,cmp[cmddep].b[4],ATMBL); /* 4: eXpansion buffer */
  468.     free(cmp[cmddep].b[4]);
  469.     cmp[cmddep].b[4] = NULL;
  470.     }
  471.     if (cmp[cmddep].b[5]) {
  472.     strncpy(atybuf,cmp[cmddep].b[5],ATMBL); /* 5: Atom buffer copY */
  473.     free(cmp[cmddep].b[5]);
  474.     cmp[cmddep].b[5] = NULL;
  475.     }
  476.     if (cmp[cmddep].b[6]) {
  477.     strncpy(filbuf,cmp[cmddep].b[6],ATMBL); /* 6: Filename buffer */
  478.     free(cmp[cmddep].b[6]);
  479.     cmp[cmddep].b[6] = NULL;
  480.     }
  481. #else
  482.     if (cmp_b[cmddep][0]) {
  483.     strncpy(cmdbuf,cmp_b[cmddep][0],CMDBL); /* 0: Command buffer */
  484.     free(cmp_b[cmddep][0]);
  485.     cmp_b[cmddep][0] = NULL;
  486.     }
  487.     if (cmp_b[cmddep][1]) {
  488.     strncpy(savbuf,cmp_b[cmddep][1],CMDBL); /* 1: Save buffer */
  489.     free(cmp_b[cmddep][1]);
  490.     cmp_b[cmddep][1] = NULL;
  491.     }
  492.     if (cmp_b[cmddep][2]) {
  493.     strncpy(hlpbuf,cmp_b[cmddep][2],HLPBL); /* 2: Help buffer */
  494.     free(cmp_b[cmddep][2]);
  495.     cmp_b[cmddep][2] = NULL;
  496.     }
  497.     if (cmp_b[cmddep][3]) {
  498.     strncpy(atmbuf,cmp_b[cmddep][3],ATMBL); /* 3: Atomic buffer! */
  499.     free(cmp_b[cmddep][3]);
  500.     cmp_b[cmddep][3] = NULL;
  501.     }
  502.     if (cmp_b[cmddep][4]) {
  503.     strncpy(atxbuf,cmp_b[cmddep][4],ATMBL); /* 4: eXpansion buffer */
  504.     free(cmp_b[cmddep][4]);
  505.     cmp_b[cmddep][4] = NULL;
  506.     }
  507.     if (cmp_b[cmddep][5]) {
  508.     strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */
  509.     free(cmp_b[cmddep][5]);
  510.     cmp_b[cmddep][5] = NULL;
  511.     }
  512.     if (cmp_b[cmddep][6]) {
  513.     strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */
  514.     free(cmp_b[cmddep][6]);
  515.     cmp_b[cmddep][6] = NULL;
  516.     }
  517. #endif /* DCMDBUF */
  518.  
  519.     cmddep--;                /* Rise, rise */
  520.     debug(F101,"&cmpop","",cmddep);
  521.     return(cmddep);
  522. }
  523. #endif /* NOSPL */
  524.  
  525. #ifdef COMMENT
  526. VOID
  527. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  528.     char *t;
  529.     while (*s) {
  530.         if (*s == CMDQ) {
  531.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  532.         }
  533.         s++;
  534.     }
  535. }
  536. #endif /* COMMENT */
  537.  
  538. /* Convert tabs to spaces, one for one */
  539. VOID
  540. untab(s) char *s; {
  541.     while (*s) {
  542.     if (*s == HT) *s = SP;
  543.     s++;
  544.     }
  545. }
  546.  
  547. /*  C M N U M  --  Parse a number in the indicated radix  */
  548.  
  549. /* 
  550.  The only radix allowed in unquoted numbers is 10.
  551.  Parses unquoted numeric strings in base 10.
  552.  Parses backslash-quoted numbers in the radix indicated by the quote:
  553.    \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal.
  554.  If these fail, then if a preprocessing function is supplied, that is applied 
  555.  and then a second attempt is made to parse an unquoted decimal string. 
  556.  
  557.  Returns:
  558.    -3 if no input present when required,
  559.    -2 if user typed an illegal number,
  560.    -1 if reparse needed,
  561.     0 otherwise, with argument n set to the number that was parsed
  562. */
  563. int
  564. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {
  565.     int x; char *s, *zp, *zq;
  566.  
  567.     if (radix != 10) {                  /* Just do base 10 */
  568.         printf("cmnum: illegal radix - %d\n",radix);
  569.         return(-1);
  570.     }
  571.     x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  572.     debug(F101,"cmnum: cmfld","",x);
  573.     if (x < 0) return(x);        /* Parse a field */
  574.     zp = atmbuf;
  575.  
  576.     if (chknum(zp)) {            /* Check for decimal number */
  577.         *n = atoi(zp);            /* Got one, we're done. */
  578.     debug(F101,"cmnum 1st chknum ok","",*n);
  579.         return(0);
  580.     } else if ((x = xxesc(&zp)) > -1) {    /* Check for backslash escape */
  581.  
  582. #ifndef OS2
  583.     *n = x;
  584. #else
  585.     *n = wideresult;
  586. #endif /* OS2 */
  587.  
  588.     debug(F101,"cmnum xxesc ok","",*n);
  589.     return(*zp ? -2 : 0);
  590.     } else if (f) {            /* If conversion function given */
  591.         zp = atmbuf;            /* Try that */
  592.     zq = atxbuf;
  593.     atxn = CMDBL;
  594.     (*f)(zp,&zq,&atxn);        /* Convert */
  595.     zp = atxbuf;
  596.     }
  597.     debug(F110,"cmnum zp",zp,0);
  598.     if (chknum(zp)) {            /* Check again for decimal number */
  599.         *n = atoi(zp);            /* Got one, we're done. */
  600.     debug(F101,"cmnum 2nd chknum ok","",*n);
  601.         return(0);
  602.     } else {                /* Not numeric */
  603.     return(-2);     
  604.     }
  605. }
  606.  
  607. /*  C M O F I  --  Parse the name of an output file  */
  608.  
  609. /*
  610.  Depends on the external function zchko(); if zchko() not available, use
  611.  cmfld() to parse output file names.
  612.  
  613.  Returns
  614.    -3 if no input present when required,
  615.    -2 if permission would be denied to create the file,
  616.    -1 if reparse needed,
  617.     0 or 1 otherwise, with xp pointing to name.
  618. */
  619. int
  620. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  621.     int x; char *s, *zq;
  622. #ifdef DTILDE
  623.     _PROTOTYP( char * tilde_expand, (char *) );
  624.     char *dirp;
  625. #endif 
  626.  
  627.     if (*xhlp == NUL) xhlp = "Output file";
  628.     *xp = "";
  629.  
  630.     if ((x = cmfld(xhlp,xdef,&s,(xx_strp)0)) < 0) return(x);
  631.  
  632.     if (f) {                /* If a conversion function is given */
  633.     zq = atxbuf;
  634.     atxn = CMDBL;
  635.     if ((x = (*f)(s,&zq,&atxn)) < 0) return(-2);
  636.     s = atxbuf;
  637.     }
  638.  
  639. #ifdef DTILDE
  640.     dirp = tilde_expand(s);        /* Expand tilde, if any, */
  641.     if (*dirp != '\0') setatm(dirp);    /* right in the atom buffer. */
  642.     s = atmbuf;
  643. #endif
  644.  
  645.     if (iswild(s)) {
  646.         printf("?Wildcards not allowed - %s\n",s);
  647.         return(-2);
  648.     }
  649.     if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* ok to write to tty */
  650.         printf("?Write permission denied - %s\n",s);
  651.         return(-9);
  652.     } else {
  653.         *xp = s;
  654.         return(x);
  655.     }
  656. }
  657.  
  658.  
  659. /*  C M I F I  --  Parse the name of an existing file  */
  660.  
  661. /*
  662.  This function depends on the external functions:
  663.    zchki()  - Check if input file exists and is readable.
  664.    zxpand() - Expand a wild file specification into a list.
  665.    znext()  - Return next file name from list.
  666.  If these functions aren't available, then use cmfld() to parse filenames.
  667. */
  668. /*
  669.  Returns
  670.    -4 EOF
  671.    -3 if no input present when required,
  672.    -2 if file does not exist or is not readable,
  673.    -1 if reparse needed,
  674.     0 or 1 otherwise, with:
  675.         xp pointing to name,
  676.         wild = 1 if name contains '*' or '?', 0 otherwise.
  677. */
  678. int
  679. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  680.     int i, x, xc; long y; char *sp, *zq, *sv;
  681. #ifdef DTILDE
  682.     char *tilde_expand(), *dirp;
  683. #endif /* DTILDE */
  684.  
  685. #ifndef NOPARTIAL
  686.     extern char *mtchs[];
  687. #endif /* NOPARTIAL */
  688.  
  689.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  690.     *xp = "";
  691.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  692.         x = gtword();                   /* No, get a word */
  693.     } else {
  694.         setatm(xdef);            /* If so, use default, if any. */
  695.     }
  696.  
  697.     *xp = atmbuf;                       /* Point to result. */
  698.  
  699.     while (1) {
  700.         xc += cc;                       /* Count the characters. */
  701.         debug(F111,"cmifi gtword",atmbuf,xc);
  702.         switch (x) {
  703.             case -4:                    /* EOF */
  704.             case -2:                    /* Out of space. */
  705.             case -1:                    /* Reparse needed */
  706.                 return(x);
  707.             case 0:                     /* SP or NL */
  708.             case 1:
  709.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  710.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  711.  
  712.         if (f) {        /* If a conversion function is given */
  713.             zq = atxbuf;    /* ... */
  714.             atxn = CMDBL;
  715.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  716.             *xp = atxbuf;
  717.         }
  718.         debug(F110,"cmifi atxbuf",atxbuf,0);
  719. #ifdef COMMENT
  720. /* don't need this stuff, zxpand does it now. */
  721. #ifdef DTILDE
  722.  
  723.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  724.         if (*dirp != '\0') setatm(dirp); /* right in atom buffer. */
  725.         *xp = atmbuf;
  726. #endif /* DTILDE */
  727.  
  728.                 /* If filespec is wild, see if there are any matches */
  729.  
  730.                 *wild = iswild(*xp);
  731.                 debug(F101,"cmifi wild","",*wild);
  732.                 if (*wild != 0) {
  733. #endif /* COMMENT */
  734.             sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */
  735.             if (!sv) {
  736.             printf("?malloc error 73, cmifi\n");
  737.             return(-9);
  738.             }
  739.             strcpy(sv,*xp);
  740.             debug(F110,"cmifi sv",sv,0);
  741.                     y = zxpand(*xp);
  742.             *wild = (y > 1);
  743.             debug(F111,"cmifi sv wild",sv,*wild);
  744.                     if (y == 0) {
  745.                         printf("?No files match - %s\n",*xp);
  746.                         return(-9);
  747.                     } else if (y < 0) {
  748.                         printf("?Too many files match - %s\n",*xp);
  749.                         return(-9);
  750.             } else if (y > 1) return(x);
  751. #ifdef COMMENT
  752.                 }
  753. #endif
  754.                 /* If not wild, see if it exists and is readable. */
  755.  
  756.         debug(F111,"cmifi sv not wild",sv,*wild);
  757.  
  758.         znext(*xp);        /* Get first (only?) matching file */
  759.                 y = zchki(*xp);        /* Check its accessibility */
  760.         zxpand(sv);        /* Rewind so next znext() gets 1st */
  761.         free(sv);        /* done with this */
  762.                 if (y == -3) {
  763.                     printf("?Read permission denied - %s\n",*xp);
  764.                     return(-9);
  765.                 } else if (y == -2) {
  766.                     printf("?File not readable - %s\n",*xp);
  767.                     return(-9);
  768.                 } else if (y < 0) {
  769.                     printf("?File not found - %s\n",*xp);
  770.                     return(-9);
  771.                 }
  772.                 return(x);
  773.  
  774. #ifndef MAC
  775.             case 2:                     /* ESC */
  776.         debug(F101,"cmifi esc, xc","",xc);
  777.                 if (xc == 0) {
  778.                     if (*xdef != '\0') {
  779.                         printf("%s ",xdef); /* If at beginning of field, */
  780. #ifdef GEMDOS
  781.             fflush(stdout);
  782. #endif /* GEMDOS */
  783.             inword = cmflgs = 0;
  784.                         addbuf(xdef);   /* supply default. */
  785.                         setatm(xdef);
  786.                     } else {            /* No default */
  787.                         putchar(BEL);
  788.                     }
  789.                     break;
  790.                 } 
  791.         if (f) {        /* If a conversion function is given */
  792.             zq = atxbuf;    /* ... */
  793.             atxn = CMDBL;
  794.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  795.                     /* reduce cc by number of \\ consumed by conversion */
  796.             /* function (needed for OS/2, where \ is path separator) */
  797.                     cc -= (strlen(*xp) - strlen(atxbuf));
  798.             *xp = atxbuf;
  799.         }
  800. /* #ifdef COMMENT */
  801. #ifdef DTILDE
  802.         dirp = tilde_expand(*xp);    /* Expand tilde, if any, */
  803.         if (*dirp != '\0') setatm(dirp); /* in the atom buffer. */
  804.                 *xp = atmbuf;
  805. #endif /* DTILDE */
  806. /* #endif */
  807.                 sp = *xp + cc;
  808. #ifdef datageneral
  809.                 *sp++ = '+';        /* Data General AOS wildcard */
  810. #else
  811.                 *sp++ = '*';        /* Others */
  812. #endif /* datageneral */
  813.                 *sp-- = '\0';
  814. #ifdef GEMDOS
  815.         if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  816.           strcat(*xp, ".*");    /* abc -> abc*.* */
  817. #endif /* GEMDOS */
  818.                 y = zxpand(*xp);    /* Add wildcard and expand list. */
  819.         if (y > 0) strcpy(filbuf,mtchs[0]);
  820.         else *filbuf = '\0';
  821.                 *sp = '\0';             /* Remove wildcard. */
  822.         *wild = (y > 1);
  823.                 if (y == 0) {
  824.                     printf("?No files match - %s\n",atmbuf);
  825.                     return(-9);
  826.                 } else if (y < 0) {
  827.                     printf("?Too many files match - %s\n",atmbuf);
  828.                     return(-9);
  829.                 } else if (y > 1) {     /* Not unique. */
  830. #ifndef NOPARTIAL
  831. /* Partial filename completion */
  832.             int i, j, k; char c;
  833.             k = 0;
  834.             debug(F111,"cmifi partial",filbuf,cc);
  835.             for (i = cc; (c = filbuf[i]); i++) {
  836.             for (j = 1; j < y; j++)
  837.               if (mtchs[j][i] != c) break;
  838.             if (j == y) k++;
  839.             else filbuf[i] = filbuf[i+1] = NUL;
  840.             }
  841.             debug(F111,"cmifi partial k",filbuf,k);
  842.             if (k > 0) {    /* Got more characters */
  843.             sp = filbuf + cc; /* Point to new ones */
  844. #ifdef VMS
  845.             for (i = 0; i < cc; i++) {
  846.                 cmdchardel(); /* Back up over old partial spec */
  847.                 bp--;
  848.             }
  849.             sp = filbuf;    /* Point to new word start */
  850.             debug(F100,"cmifi vms erase ok","",0);
  851. #endif /* VMS */
  852.             cc = k;        /* How many new ones we just got */
  853.             printf("%s",sp);        /* Print them */
  854.             while (*bp++ = *sp++) ;    /* Copy to command buffer */
  855.             bp--;                    /* Back up over NUL */
  856.             debug(F110,"cmifi partial cmdbuf",cmdbuf,0);
  857.             setatm(filbuf);
  858.             debug(F111,"cmifi partial atmbuf",atmbuf,cc);
  859.             *xp = atmbuf;
  860.             }
  861. #endif /* NOPARTIAL */
  862.             putchar(BEL);    /* Beep because not unique. */
  863.                 } else {                /* Unique, complete it.  */
  864.                     sp = filbuf + cc;   /* Point past what user typed. */
  865. #ifdef VMS
  866.             for (i = 0; i < cc; i++) {
  867.             cmdchardel();    /* Back up over old partial spec */
  868.             bp--;
  869.             }
  870.             sp = filbuf;    /* Point to new word start */
  871. #endif /* VMS */
  872.                     printf("%s ",sp);   /* Complete the name. */
  873. #ifdef GEMDOS
  874.             fflush(stdout);
  875. #endif /* GEMDOS */
  876.                     addbuf(sp);         /* Add the characters to cmdbuf. */
  877.                     setatm(filbuf);    /* And to atmbuf. */
  878.             inword = cmflgs = 0;
  879.                     *xp = atmbuf;       /* Return pointer to atmbuf. */
  880.                     return(0);
  881.                 }
  882.                 break;
  883.  
  884.             case 3:                     /* Question mark */
  885.                 if (*xhlp == NUL)
  886.           printf(" Input file specification");
  887.                 else
  888.           printf(" %s",xhlp);
  889. #ifdef GEMDOS
  890.         fflush(stdout);
  891. #endif /* GEMDOS */
  892.                 if (xc > 0) {
  893.             if (f) {        /* If a conversion function is given */
  894.             zq = atxbuf;    /* ... */
  895.             atxn = CMDBL;
  896.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  897.             *xp = atxbuf;
  898.             }
  899. #ifdef DTILDE
  900.             dirp = tilde_expand(*xp);    /* Expand tilde, if any */
  901.             if (*dirp != '\0') setatm(dirp);
  902.             *xp = atmbuf;
  903. #endif
  904.             debug(F111,"cmifi ? *xp, cc",*xp,cc);
  905.                     sp = *xp + cc;    /* Insert "*" at end */
  906. #ifdef datageneral
  907.                     *sp++ = '+';        /* Insert +, the DG wild card */
  908. #else
  909.                     *sp++ = '*';
  910. #endif /* datageneral */
  911.                     *sp-- = '\0';
  912. #ifdef GEMDOS
  913.             if (! strchr(*xp, '.'))    /* abde.e -> abcde.e* */
  914.               strcat(*xp, ".*");    /* abc -> abc*.* */
  915. #endif /* GEMDOS */
  916.             debug(F110,"cmifi ? wild",*xp,0);
  917.                     y = zxpand(*xp);
  918.                     *sp = '\0';
  919.                     if (y == 0) {                   
  920.                         printf("?No files match - %s\n",atmbuf);
  921.                         return(-9);
  922.                     } else if (y < 0) {
  923.                         printf("?Too many files match - %s\n",atmbuf);
  924.                         return(-9);
  925.                     } else {
  926.                         printf(", one of the following:\n");
  927.                         clrhlp();
  928.                         for (i = 0; i < y; i++) {
  929.                             znext(filbuf);
  930. #ifdef VMS
  931.                 printf(" %s\n",filbuf); /* VMS names can be long */
  932. #else
  933.                             addhlp(filbuf);
  934. #endif /* VMS */
  935.                         }
  936.                         dmphlp();
  937.                     }
  938.                 } else printf("\n");
  939.                 printf("%s%s",cmprom,cmdbuf);
  940.         fflush(stdout);
  941.                 break;
  942. #endif /* MAC */
  943.         }
  944.     x = gtword();
  945.     *xp = atmbuf;
  946.     }
  947. }
  948.  
  949. /*  C M D I R  --  Parse a directory specification  */
  950.  
  951. /*
  952.  This function depends on the external functions:
  953.    zchki()  - Check if input file exists and is readable.
  954.  If these functions aren't available, then use cmfld() to parse dir names.
  955.  Note: this function quickly cobbled together, mainly by deleting lots of
  956.  lines from cmifi().  It seems to work, but various services are missing,
  957.  like completion, lists of matching directories on "?", etc.
  958. */
  959. /*
  960.  Returns
  961.    -4 EOF
  962.    -3 if no input present when required,
  963.    -2 if out of space or other internal error,
  964.    -1 if reparse needed,
  965.     0 or 1, with xp pointing to name, if directory specified,
  966.     2 if a wildcard was included.
  967. */
  968. int
  969. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  970.     int x, xc; char *zq;
  971. #ifdef DTILDE
  972.     char *tilde_expand(), *dirp;
  973. #endif /* DTILDE */
  974.  
  975.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  976.     *xp = "";
  977.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  978.         x = gtword();                   /* No, get a word */
  979.     } else {
  980.         setatm(xdef);            /* If so, use default, if any. */
  981.     }
  982.     *xp = atmbuf;                       /* Point to result. */
  983.     while (1) {
  984.         xc += cc;                       /* Count the characters. */
  985.         debug(F111,"cmdir gtword",atmbuf,xc);
  986.         switch (x) {
  987.             case -4:                    /* EOF */
  988.             case -2:                    /* Out of space. */
  989.             case -1:                    /* Reparse needed */
  990.                 return(x);
  991.             case 0:                     /* SP or NL */
  992.             case 1:
  993.                 if (xc == 0) *xp = xdef;     /* If no input, return default. */
  994.         else *xp = atmbuf;
  995.                 if (**xp == NUL) return(-3); /* If field empty, return -3. */
  996.  
  997.         if (f) {        /* If a conversion function is given */
  998.             zq = atxbuf;    /* ... */
  999.             atxn = CMDBL;
  1000.             if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1001.             *xp = atxbuf;
  1002.             cc = (int)strlen(atxbuf);
  1003.         }
  1004. #ifdef DTILDE
  1005. /*
  1006.   This is ugly, and for UNIX only.
  1007.   Normally, we wouldn't call tilde_expand from a place like this anyway,
  1008.   but rather let zxpand() take care of it.  But in this case we might want 
  1009.   a hybrid result -- a string with the tilde expanded, but with wildcards
  1010.   left unexpanded.
  1011. */
  1012.         dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1013.         if (*dirp == '~') {    /* Still starts with tilde? */
  1014.             char *tp;        /* Yes, convert to lowercase */
  1015.             tp = *xp;        /* and try again. */
  1016.             while (*tp) {
  1017.             if (isupper(*tp)) *tp = tolower(*tp);
  1018.             tp++;
  1019.             }
  1020.         }
  1021.         dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1022.         if (*dirp != '\0') setatm(dirp); /* in the atom buffer. */
  1023.         *xp = atmbuf;
  1024. #endif /* DTILDE */
  1025.         if (iswild(*xp)) return(2);
  1026.         else return(x);
  1027.  
  1028.             case 2:                     /* ESC */
  1029.         putchar(BEL);
  1030.         break;
  1031.  
  1032.             case 3:                     /* Question mark */
  1033.                 if (*xhlp == NUL)
  1034.                     printf(" Directory name");
  1035.                 else
  1036.                     printf(" %s",xhlp);
  1037.                 printf("\n%s%s",cmprom,cmdbuf);
  1038.         fflush(stdout);
  1039.                 break;
  1040.         }
  1041.     x = gtword();
  1042. /*  *xp = atmbuf;  */
  1043.     }
  1044. }
  1045.  
  1046. /*  C M F L D  --  Parse an arbitrary field  */
  1047. /*
  1048.  Returns
  1049.    -3 if no input present when required,
  1050.    -2 if field too big for buffer,
  1051.    -1 if reparse needed,
  1052.     0 otherwise, xp pointing to string result.
  1053. */
  1054. int
  1055. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1056.     int x, xc;
  1057.     char *zq;
  1058.  
  1059.     inword = cc = xc = 0;        /* Initialize counts & pointers */
  1060.     *xp = "";
  1061.     if ((x = cmflgs) != 1) {            /* Already confirmed? */
  1062.         x = gtword();                   /* No, get a word */
  1063.     } else {
  1064.         setatm(xdef);            /* If so, use default, if any. */
  1065.     }
  1066.     *xp = atmbuf;                       /* Point to result. */
  1067.  
  1068.     while (1) {
  1069.         xc += cc;                       /* Count the characters. */
  1070.         debug(F111,"cmfld: gtword",atmbuf,xc);
  1071.         debug(F101,"cmfld x","",x);
  1072.         switch (x) {
  1073.             case -4:                    /* EOF */
  1074.             case -2:                    /* Out of space. */
  1075.             case -1:                    /* Reparse needed */
  1076.                 return(x);
  1077.             case 0:                     /* SP or NL */
  1078.             case 1:
  1079.                 if (xc == 0)         /* If no input, return default. */
  1080.           setatm(xdef);
  1081.         *xp = atmbuf;
  1082.         if (f) {        /* If a conversion function is given */
  1083.             zq = atxbuf;    /* ... */
  1084.             atxn = CMDBL;
  1085.             if ((*f)(*xp,&zq,&atxn) < 0) return(-2);
  1086.             setatm(atxbuf);
  1087.             *xp = atmbuf;
  1088.         }
  1089.                 if (**xp == NUL) {    /* If variable evaluates to null */
  1090.             setatm(xdef);    /* Stick in the default again. */
  1091.             if (**xp == NUL) x = -3; /* If still empty, return -3. */
  1092.         }
  1093. #ifdef COMMENT
  1094. /* The following is apparently not necessary. */
  1095. /* Remove it if nothing is broken, esp. TAKE file with trailing comments */
  1096.         xx = *xp;
  1097.         debug(F111,"cmfld before trim",*xp,x);
  1098.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  1099.           if (xx[i] != SP)    /* Trim trailing blanks */
  1100.             break;
  1101.           else
  1102.             xx[i] = NUL;
  1103.         debug(F111,"cmfld returns",*xp,x); 
  1104. #endif /* COMMENT */
  1105.         debug(F101,"cmfld: returns","",x);
  1106.                 return(x);
  1107.             case 2:                     /* ESC */
  1108.                 if (xc == 0 && *xdef != NUL) {
  1109.                     printf("%s ",xdef); /* If at beginning of field, */
  1110. #ifdef GEMDOS
  1111.             fflush(stdout);
  1112. #endif /* GEMDOS */
  1113.                     addbuf(xdef);       /* supply default. */
  1114.             inword = cmflgs = 0;
  1115.                     setatm(xdef);    /* Return as if whole field */
  1116.                     return(0);          /* typed, followed by space. */
  1117.                 } else {
  1118.                     putchar(BEL);       /* Beep if already into field. */
  1119.                 }                   
  1120.                 break;
  1121.             case 3:                     /* Question mark */
  1122.                 if (*xhlp == NUL)
  1123.                     printf(" Please complete this field");
  1124.                 else
  1125.                     printf(" %s",xhlp);
  1126.                 printf("\n%s%s",cmprom,cmdbuf);
  1127.         fflush(stdout);
  1128.                 break;
  1129.         }
  1130.     x = gtword();
  1131. /*  *xp = atmbuf; */
  1132.     }
  1133. }
  1134.  
  1135.  
  1136. /*  C M T X T  --  Get a text string, including confirmation  */
  1137.  
  1138. /*
  1139.   Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  1140.   string typed.  Returns
  1141.  
  1142.    -1 if reparse needed or buffer overflows.
  1143.     1 otherwise.
  1144.  
  1145.   with cmflgs set to return code, and xp pointing to result string.
  1146. */
  1147. int
  1148. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; xx_strp f; {
  1149.  
  1150.     int x, i;
  1151.     char *xx, *zq;
  1152.     static int xc;
  1153.  
  1154.     debug(F101,"cmtxt, cmflgs","",cmflgs);
  1155.     inword = cc = 0;            /* Start atmbuf counter off at 0 */
  1156.     if (cmflgs == -1) {                 /* If reparsing, */
  1157.         xc = (int)strlen(*xp);        /* get back the total text length, */
  1158.     } else {                            /* otherwise, */
  1159.         *xp = "";                       /* start fresh. */
  1160.         xc = 0;
  1161.     }
  1162.     *atmbuf = NUL;                      /* And empty the atom buffer. */
  1163.     if ((x = cmflgs) != 1) {
  1164.         x = gtword();                   /* Get first word. */
  1165.         *xp = pp;                       /* Save pointer to it. */
  1166.     }
  1167.     debug(F101,"cmtxt (*f)","", f);
  1168.     while (1) {                /* Loop for each word in text. */
  1169.         xc += cc;                       /* Char count for all words. */
  1170.         debug(F111,"cmtxt: gtword",atmbuf,xc);
  1171.         debug(F101," x","",x);
  1172.         switch (x) {
  1173.       case -9:            /* Buffer overflow */
  1174.       case -4:            /* EOF */
  1175. #ifdef MAC
  1176.       case -3:            /* Quit/Timeout */
  1177. #endif /* MAC */
  1178.       case -2:            /* Overflow */
  1179.       case -1:            /* Deletion */
  1180.         return(x);
  1181.       case 0:            /* Space */
  1182.         xc++;            /* Just count it */
  1183.         break;
  1184.       case 1:            /* CR or LF */
  1185.         if (xc == 0) *xp = xdef;
  1186.         if (f) {            /* If a conversion function is given */
  1187.         zq = atxbuf;        /* Point to the expansion buffer */
  1188.         atxn = CMDBL;        /* specify its length */
  1189.         debug(F110,"cmtxt calling (*f)",*xp,0);
  1190.         if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  1191.         cc = (int)strlen(atxbuf);
  1192.         *xp = atxbuf;        /* and return pointer to it. */
  1193.         debug(F111,"cmtxt (*f) returns",*xp,cc);
  1194.         }
  1195.         xx = *xp;
  1196.         for (i = (int)strlen(xx) - 1; i > 0; i--)
  1197.           if (xx[i] != SP)        /* Trim trailing blanks */
  1198.         break;
  1199.           else
  1200.         xx[i] = NUL;
  1201.         return(x);
  1202.       case 2:            /* ESC */
  1203.         if (xc == 0) {
  1204.         printf("%s ",xdef);
  1205.         inword = cmflgs = 0;
  1206. #ifdef GEMDOS
  1207.         fflush(stdout);
  1208. #endif /* GEMDOS */
  1209.         cc = addbuf(xdef);
  1210.         } else {
  1211.         putchar(BEL);
  1212.         }
  1213.         break;
  1214.       case 3:            /* Question Mark */
  1215.         if (*xhlp == NUL)
  1216.           printf(" Text string");
  1217.         else
  1218.           printf(" %s",xhlp);
  1219.         printf("\n%s%s",cmprom,cmdbuf);
  1220.         fflush(stdout);
  1221.         break;
  1222.       default:
  1223.         printf("?Unexpected return code from gtword() - %d\n",x);
  1224.         return(-2);
  1225.         }
  1226.         x = gtword();
  1227.     }
  1228. }
  1229.  
  1230.  
  1231. /*  C M K E Y  --  Parse a keyword  */
  1232.  
  1233. /*
  1234.  Call with:
  1235.    table    --  keyword table, in 'struct keytab' format;
  1236.    n        --  number of entries in table;
  1237.    xhlp     --  pointer to help string;
  1238.    xdef     --  pointer to default keyword;
  1239.  
  1240.  Returns:
  1241.    -3       --  no input supplied and no default available
  1242.    -2       --  input doesn't uniquely match a keyword in the table
  1243.    -1       --  user deleted too much, command reparse required
  1244.     n >= 0  --  value associated with keyword
  1245. */
  1246. int
  1247. cmkey(table,n,xhlp,xdef,f)
  1248. /* cmkey */  struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  1249.     return(cmkey2(table,n,xhlp,xdef,"",f));
  1250. }
  1251. int
  1252. cmkey2(table,n,xhlp,xdef,tok,f)
  1253.     struct keytab table[]; int n; char *xhlp, *xdef; char *tok; xx_strp f; {
  1254.  
  1255.     int i, tl, y, z, zz, xc;
  1256.     char *xp, *zq;
  1257.  
  1258.     tl = (int)strlen(tok);
  1259.     inword = xc = cc = 0;        /* Clear character counters. */
  1260.  
  1261.     if ((zz = cmflgs) == 1)             /* Command already entered? */
  1262.       setatm(xdef);            /* Yes, copy default into atom buf */
  1263.     else zz = gtword();            /* Otherwise get a command word */
  1264.  
  1265. debug(F101,"cmkey: table length","",n);
  1266. debug(F101," cmflgs","",cmflgs);
  1267. debug(F101," zz","",zz);
  1268. while (1) {
  1269.     xc += cc;
  1270.     debug(F111,"cmkey: gtword",atmbuf,xc);
  1271.  
  1272.     switch(zz) {
  1273.         case -4:                        /* EOF */
  1274. #ifdef MAC
  1275.     case -3:            /* Quit/Timeout */
  1276. #endif /* MAC */
  1277.         case -2:                        /* Buffer overflow */
  1278.         case -1:                        /* Or user did some deleting. */
  1279.             return(cmflgs = zz);
  1280.  
  1281.         case 0:                         /* User terminated word with space */
  1282.         case 1:                         /* or newline */
  1283.             if (cc == 0) setatm(xdef);    /* Supply default if user typed nada */
  1284.         if (f) {            /* If a conversion function is given */
  1285.         zq = atxbuf;        /* apply it */
  1286.         atxn = CMDBL;
  1287.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1288.         debug(F110,"cmkey atxbuf after *f",atxbuf,0);
  1289.         setatm(atxbuf);
  1290.         }
  1291.             y = lookup(table,atmbuf,n,&z); /* Look up the word in the table */
  1292.             switch (y) {
  1293.                 case -2:        /* Ambiguous */
  1294.                     printf("?Ambiguous - %s\n",atmbuf);
  1295.             cmflgs = -2;
  1296.                     return(-9);
  1297.                 case -1:        /* Not found at all */
  1298.             if (tl) {
  1299.             for (i = 0; i < tl; i++) /* Check for token */
  1300.               if (tok[i] == *atmbuf) { /* Got one */
  1301.                   ungword();  /* Put back the following word */
  1302.                   return(-5); /* Special return code for token */
  1303.               }
  1304.             }
  1305.             /* Kludge alert... only print error if */
  1306.             /* we were called as cmkey2, but not cmkey... */
  1307.             /* This doesn't seem to always work. */
  1308.             if (tl == 0) {
  1309.             printf("?No keywords match - %s\n",atmbuf); /* cmkey */
  1310.             return(cmflgs = -9);
  1311.             } else { 
  1312.             if (cmflgs == 1) return(cmflgs = -6); /* cmkey2 */
  1313.             else return(cmflgs = -2);
  1314.             /* The -6 code is to let caller try another table */
  1315.             }
  1316.                 default:
  1317.                     break;
  1318.         }
  1319.             return(y);
  1320.  
  1321.         case 2:                         /* User terminated word with ESC */
  1322.             if (cc == 0) {
  1323.                 if (*xdef != NUL) {     /* Nothing in atmbuf */
  1324.                     printf("%s ",xdef); /* Supply default if any */
  1325. #ifdef GEMDOS
  1326.             fflush(stdout);
  1327. #endif /* GEMDOS */
  1328.                     addbuf(xdef);
  1329.                     setatm(xdef);
  1330.             inword = cmflgs = 0;
  1331.                     debug(F111,"cmkey: default",atmbuf,cc);
  1332.                 } else {
  1333.                     putchar(BEL);       /* No default, just beep */
  1334.                     break;
  1335.                 }
  1336.             }
  1337.         if (f) {            /* If a conversion function is given */
  1338.         zq = atxbuf;        /* apply it */
  1339.         atxn = CMDBL;
  1340.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1341.         setatm(atxbuf);
  1342.         }
  1343.             y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  1344.             debug(F111,"cmkey: esc",atmbuf,y);
  1345.             if (y == -2) {        /* Ambiguous */
  1346.                 putchar(BEL);
  1347.                 break;
  1348.             }
  1349.             if (y == -1) {        /* Not found */
  1350.                 /* if (tl == 0) */ printf("?No keywords match - %s\n",atmbuf);
  1351.         cmflgs = -2;
  1352.                 return(-9);
  1353.             }
  1354. /*
  1355.   See if the keyword just found has the CM_ABR bit set in its flgs field, and
  1356.   if so, search forwards in the table for a keyword that has the same kwval
  1357.   but does not have CM_ABR (or CM_INV?) set, and then expand using the full
  1358.   keyword.  WARNING: This assumes that (a) keywords are in alphabetical order,
  1359.   and (b) the CM_ABR bit is set only if the the abbreviated keyword is a true
  1360.   abbreviation (left substring) of the full keyword.
  1361. */
  1362.         if (test(table[z].flgs,CM_ABR)) {
  1363.         int zz;
  1364.         for (zz = z+1; zz < n; zz++)
  1365.           if ((table[zz].kwval == table[z].kwval) &&
  1366.               (!test(table[zz].flgs,CM_ABR))) {
  1367.               z = zz;
  1368.               break;
  1369.           }
  1370.             } 
  1371.             xp = table[z].kwd + cc;
  1372.             printf("%s ",xp);
  1373. #ifdef GEMDOS
  1374.         fflush(stdout);
  1375. #endif /* GEMDOS */
  1376.             addbuf(xp);
  1377.         inword = cmflgs = 0;
  1378.             debug(F110,"cmkey: addbuf",cmdbuf,0);
  1379.             return(y);
  1380.  
  1381.         case 3:                         /* User typed "?" */
  1382.         if (f) {            /* If a conversion function is given */
  1383.         zq = atxbuf;        /* do the conversion now. */
  1384.         atxn = CMDBL;
  1385.         if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  1386.         setatm(atxbuf);
  1387.         }
  1388.             y = lookup(table,atmbuf,n,&z); /* Look up what we have so far. */
  1389.  
  1390.         if (y == -1) {
  1391.                 /* if (tl == 0) */ printf(" No keywords match\n");
  1392.         cmflgs = -2;
  1393.                 return(-9);
  1394.             }
  1395.             if (*xhlp == NUL)
  1396.                 printf(" One of the following:\n");
  1397.             else
  1398.                 printf(" %s, one of the following:\n",xhlp);
  1399.  
  1400.         if ((y > -1) &&
  1401.         !test(table[z].flgs,CM_ABR) &&
  1402.         ((z >= n-1) || strncmp(table[z].kwd,table[z+1].kwd,cc))
  1403.          ) {
  1404.         printf(" %s\n",table[z].kwd);
  1405.         } else {
  1406.         clrhlp();
  1407.         for (i = 0; i < n; i++) {   
  1408.             if (!strncmp(table[i].kwd,atmbuf,cc)
  1409.             && !test(table[i].flgs,CM_INV)
  1410.             )
  1411.               addhlp(table[i].kwd);
  1412.         }
  1413.         dmphlp();
  1414.         }
  1415.         if (*atmbuf == NUL) {
  1416.         if (tl == 1)
  1417.           printf("or the token '%c'\n",*tok);
  1418.         else if (tl > 1) printf("or one of the tokens '%s'\n",tok);
  1419.         }
  1420.             printf("%s%s", cmprom, cmdbuf);
  1421.         fflush(stdout);
  1422.             break;
  1423.  
  1424.         default:            
  1425.             printf("\n%d - Unexpected return code from gtword\n",zz);
  1426.             return(cmflgs = -2);
  1427.         }
  1428.         zz = gtword();
  1429.     }
  1430. }
  1431. int
  1432. chktok(tlist) char *tlist; {
  1433.     char *p;
  1434.     p = tlist;
  1435.     while (*p != NUL && *p != *atmbuf) p++;
  1436.     return((*p) ? (int) *p : 0);
  1437. }
  1438.  
  1439. /*  C M C F M  --  Parse command confirmation (end of line)  */
  1440.  
  1441. /*
  1442.  Returns
  1443.    -2: User typed anything but whitespace or newline
  1444.    -1: Reparse needed
  1445.     0: Confirmation was received
  1446. */
  1447. int
  1448. cmcfm() {
  1449.     int x, xc;
  1450.  
  1451.     debug(F101,"cmcfm: cmflgs","",cmflgs);
  1452.     debug(F110,"cmcfm: atmbuf",atmbuf,0);
  1453.     inword = xc = cc = 0;
  1454.     if (cmflgs == 1) return(0);
  1455.  
  1456.     setatm("");                /* (Probably unnecessary) */
  1457.  
  1458.     while (1) {
  1459.         x = gtword();
  1460.         xc += cc;
  1461.         switch (x) {
  1462.             case -4:                    /* EOF */
  1463.             case -2:
  1464.             case -1:
  1465.                 return(x);
  1466.  
  1467.             case 1:                     /* End of line */
  1468.                 if (xc > 0) {
  1469.                     printf("?Not confirmed - %s\n",atmbuf);
  1470.                     return(-9);
  1471.                 } else return(0);                   
  1472.             case 2:            /* ESC */
  1473.         if (xc == 0) {
  1474.             putchar(BEL);    /* beep & continue */
  1475.             continue;        /* or fall thru. */
  1476.         }
  1477.             case 0:                     /* Space */
  1478.         if (xc == 0)        /* If no chars typed, continue, */
  1479.           continue;        /* else fall thru. */
  1480.             case 3:            /* Question mark */
  1481.                 if (xc > 0) {
  1482.                     printf("?Not confirmed - %s\n",atmbuf);
  1483.                     return(-9);
  1484.                 }
  1485.                 printf("\n Type a carriage return to confirm the command\n");
  1486.                 printf("%s%s",cmprom,cmdbuf);
  1487.         fflush(stdout);
  1488.                 continue;
  1489.         }
  1490.     }
  1491. }
  1492.  
  1493. /* Keyword help routines */
  1494.  
  1495.  
  1496. /*  C L R H L P -- Initialize/Clear the help line buffer  */
  1497.  
  1498. static VOID
  1499. clrhlp() {                              /* Clear the help buffer */
  1500.     hlpbuf[0] = NUL;
  1501.     hh = hx = 0;
  1502. }
  1503.  
  1504.  
  1505. /*  A D D H L P  --  Add a string to the help line buffer  */
  1506.  
  1507. static VOID
  1508. addhlp(s) char *s; {                    /* Add a word to the help buffer */
  1509.     int j;
  1510.  
  1511.     hh++;                               /* Count this column */
  1512.  
  1513.     for (j = 0; (j < hc) && (*s != NUL); j++) { /* Fill the column */
  1514.         hlpbuf[hx++] = *s++;
  1515.     }
  1516.     if (*s != NUL)                      /* Still some chars left in string? */
  1517.         hlpbuf[hx-1] = '+';             /* Mark as too long for column. */
  1518.  
  1519.     if (hh < (hw / hc)) {               /* Pad col with spaces if necessary */
  1520.         for (; j < hc; j++) {
  1521.             hlpbuf[hx++] = SP;
  1522.         }
  1523.     } else {                            /* If last column, */
  1524.         hlpbuf[hx++] = NUL;             /* no spaces. */
  1525.         dmphlp();                       /* Print it. */
  1526.         return;
  1527.     }
  1528. }
  1529.  
  1530.  
  1531. /*  D M P H L P  --  Dump the help line buffer  */
  1532.  
  1533. static VOID
  1534. dmphlp() {                              /* Print the help buffer */
  1535.     hlpbuf[hx++] = NUL;
  1536.     printf(" %s\n",hlpbuf);
  1537.     clrhlp();
  1538. }
  1539.  
  1540.  
  1541. /*  G T W O R D  --  Gets a "word" from the command input stream  */
  1542.  
  1543. /*
  1544. Usage: retcode = gtword();
  1545.  
  1546. Returns:
  1547.  -4 if end of file (e.g. pipe broken)
  1548.  -2 if command buffer overflows
  1549.  -1 if user did some deleting
  1550.   0 if word terminates with SP or tab
  1551.   1 if ... CR
  1552.   2 if ... ESC
  1553.   3 if ... ? (question mark)
  1554.  
  1555. With:
  1556.   pp pointing to beginning of word in buffer
  1557.   bp pointing to after current position
  1558.   atmbuf containing a copy of the word
  1559.   cc containing the number of characters in the word copied to atmbuf
  1560. */
  1561.  
  1562. int
  1563. ungword() {                /* unget a word */
  1564.     if (ungw) return(0);
  1565.     cmfsav = cmflgs;
  1566.     debug(F101,"ungword cmflgs","",cmflgs);
  1567.     ungw = 1;
  1568.     cmflgs = 0;
  1569.     return(0);
  1570. }
  1571.  
  1572. static int
  1573. gtword() {
  1574.     int c;                              /* Current char */
  1575.     int quote = 0;                      /* Flag for quote character */
  1576.     int echof = 0;                      /* Flag for whether to echo */
  1577.     int chsrc = 0;            /* Source of character, 1 = tty */
  1578.     int comment = 0;            /* Flag for in comment */
  1579.     char *cp = NULL;            /* Comment pointer */
  1580.  
  1581. #ifdef RTU
  1582.     extern int rtu_bug;
  1583. #endif /* RTU */
  1584.  
  1585. #ifdef datageneral
  1586.     extern int termtype;                /* DG terminal type flag */
  1587.     extern int con_reads_mt;            /* Console read asynch is active */
  1588.     if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */
  1589. #endif /* datageneral */
  1590.  
  1591.     if (ungw) {                /* Have a word saved? */
  1592.     debug(F110,"gtword ungetting from pp",pp,0);
  1593.     while (*pp++ == SP) ;
  1594.     setatm(pp);
  1595.     ungw = 0;
  1596.     cmflgs = cmfsav;
  1597.     debug(F111,"gtword returning atmbuf",atmbuf,cmflgs);
  1598.     return(cmflgs);
  1599.     }
  1600.     pp = np;                            /* Start of current field */
  1601.  
  1602.     debug(F111,"gtword: cmdbuf",cmdbuf,cmdbuf);
  1603.     debug(F111," bp",bp,bp);
  1604.     debug(F111," pp",pp,pp);
  1605.  
  1606.     while (bp < cmdbuf+CMDBL) {         /* Big get-a-character loop */
  1607.     echof = 0;            /* Assume we don't echo because */
  1608.     chsrc = 0;            /* character came from reparse buf. */
  1609.  
  1610.         if ((c = *bp) == NUL) {         /* If no char waiting in reparse buf */
  1611.             if (dpx) echof = 1;         /* must get from tty, set echo flag. */
  1612.         c = cmdgetc();        /* Read a character from the tty. */
  1613.         chsrc = 1;            /* Remember character source is tty. */
  1614. #ifdef MAC
  1615.         if (c == -3)        /* If null command... */
  1616.           return(-3);
  1617. #endif /* MAC */
  1618.             if (c == EOF) {        /* This can happen if stdin not tty. */
  1619. #ifdef EINTR
  1620.         if (errno == EINTR)    /* This is for when bg'd process is */
  1621.           continue;        /* fg'd again. */
  1622. #endif /* EINTR */
  1623.         return(-4);
  1624.         }
  1625.         c &= cmdmsk;        /* Strip any parity bit */
  1626.     }                /* if desired. */
  1627. #ifndef MAC
  1628.     debug(F000,"gtword char","",c);
  1629. #endif /* MAC */
  1630.  
  1631. /* Now we have the next character */
  1632.  
  1633.         if (quote == 0) {        /* If this is not a quoted character */
  1634.             if (c == CMDQ) {        /* Got the quote character itself */
  1635.         if (!comment) quote = 1; /* Flag it if not in a comment */
  1636.             }
  1637.         if (c == FF) {        /* Formfeed. */
  1638.                 c = NL;                 /* Replace with newline */
  1639. #ifdef COMMENT
  1640. /* No more screen clearing... */
  1641.         cmdclrscn();        /* Clear the screen */
  1642. #endif /* COMMENT */
  1643.             }
  1644.         if (c == HT) {        /* Tab */
  1645.         if (comment)        /* If in comment, */
  1646.           c = SP;        /* substitute space */
  1647.         else            /* otherwise */
  1648.           c = ESC;        /* substitute ESC (for completion) */
  1649.         }
  1650.         if (c == ';' || c == '#') { /* Trailing comment */
  1651.         if (inword == 0) {    /* If we're not in a word */
  1652.             comment = 1;    /* start a comment. */
  1653.             cp = bp;        /* remember where it starts. */
  1654.         }
  1655.         }
  1656.         if (!comment && c == SP) {    /* Space */
  1657.                 *bp++ = c;        /* deposit in buffer if not already */
  1658.                 if (echof) putchar(c);  /* echo it. */
  1659.                 if (inword == 0) {      /* If leading, gobble it. */
  1660.                     pp++;
  1661.                     continue;
  1662.                 } else {                /* If terminating, return. */
  1663.                     np = bp;
  1664.                     setatm(pp);
  1665.                     inword = cmflgs = 0;
  1666.             return(0);
  1667.                 }
  1668.             }
  1669.             if (c == NL || c == CR) {   /* CR or LF. */
  1670.         if (echof) cmdnewl((char)c); /* Echo it. */
  1671.         while (bp > pp && (*(bp-1) == SP || *(bp-1) == HT)) /* Trim */
  1672.           bp--;            /* trailing */
  1673.         *bp = NUL;        /* whitespace. */
  1674.         if ((bp > pp) && (*(bp-1) == '-')) { /* This line continued? */
  1675.             if (chsrc) {    /* If reading from tty, */
  1676. #ifdef COMMENT
  1677.             bp--, pp--;    /* back up the buffer pointer, */
  1678. #else
  1679.             bp--;
  1680. #endif /* COMMENT */
  1681.             *bp = NUL;    /* erase the dash, */
  1682.             continue;    /* and go back for next char now. */
  1683.             }
  1684.         } else {        /* No, a command has been entered. */
  1685.             *bp = NUL;        /* Terminate the command string. */
  1686.             if (comment) {    /* If we're in a comment, */
  1687.             comment = 0;    /* Say we're not any more, */
  1688.             *cp = NUL;    /* cut it off. */
  1689.             }
  1690.             np = bp;        /* Where to start next field. */
  1691.             setatm(pp);        /* Copy this field to atom buffer. */
  1692.             inword = 0;        /* Not in a word any more. */
  1693.             return(cmflgs = 1);
  1694.         }
  1695.             }
  1696.             if (!comment && echof && (c == '?')) { /* Question mark */
  1697.                 putchar(c);
  1698.                 *bp = NUL;
  1699.                 setatm(pp);
  1700.                 return(cmflgs = 3);
  1701.             }
  1702.             if (c == ESC) {        /* ESC */
  1703.         if (!comment) {
  1704.             *bp = NUL;
  1705.             setatm(pp);
  1706.             return(cmflgs = 2);
  1707.         } else {
  1708.             putchar(BEL);
  1709.             continue;
  1710.         }
  1711.             }
  1712.             if (c == BS || c == RUB) {  /* Character deletion */
  1713.                 if (bp > cmdbuf) {      /* If still in buffer... */
  1714.             cmdchardel();    /* erase it. */
  1715.                     bp--;               /* point behind it, */
  1716.                     if (*bp == SP) inword = 0; /* Flag if current field gone */
  1717.                     *bp = NUL;          /* Erase character from buffer. */
  1718.                 } else {                /* Otherwise, */
  1719.                     putchar(BEL);       /* beep, */
  1720.                     cmres();            /* and start parsing a new command. */
  1721.             *bp = *atmbuf = NUL;
  1722.                 }
  1723.                 if (pp < bp) continue;
  1724.                 else return(cmflgs = -1);
  1725.             }
  1726.             if (c == LDEL) {            /* ^U, line deletion */
  1727.                 while ((bp--) > cmdbuf) {
  1728.                     cmdchardel();
  1729.                     *bp = NUL;
  1730.                 }
  1731.                 cmres();                /* Restart the command. */
  1732.         *bp = *atmbuf = NUL;
  1733.                 inword = 0;
  1734.                 return(cmflgs = -1);
  1735.             }
  1736.             if (c == WDEL) {            /* ^W, word deletion */
  1737.                 if (bp <= cmdbuf) {     /* Beep if nothing to delete */
  1738.                     putchar(BEL);
  1739.                     cmres();
  1740.             *bp = *atmbuf = NUL;
  1741.                     return(cmflgs = -1);
  1742.                 }
  1743.                 bp--;
  1744.                 for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) {
  1745.                     cmdchardel();
  1746.                     *bp = NUL;
  1747.                 }
  1748.                 for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) {
  1749.                     cmdchardel();
  1750.                     *bp = NUL;
  1751.                 }
  1752.                 bp++;
  1753.                 inword = 0;
  1754.                 return(cmflgs = -1);
  1755.             }
  1756.             if (c == RDIS) {            /* ^R, redisplay */
  1757. #ifdef COMMENT
  1758.                 *bp = NUL;
  1759.                 printf("\n%s%s",cmprom,cmdbuf);
  1760. #else
  1761.         char *cpx; char cx;
  1762.                 *bp = NUL;
  1763.                 printf("\n%s",cmprom);
  1764.         cpx = cmdbuf;
  1765.         while (cx = *cpx++) {
  1766. #ifdef isprint
  1767.             putchar(isprint(cx) ? cx : '^');
  1768. #else
  1769.             putchar((cx >= SP && cx < DEL) ? cx : '^');
  1770. #endif /* isprint */
  1771.         }
  1772. #endif /* COMMENT */
  1773.         fflush(stdout);
  1774.                 continue;
  1775.             }
  1776.         if (c < SP && quote == 0) {    /* Any other unquoted control char */
  1777.         if (!chsrc) bp++;    /* If cmd file, point past it */
  1778.         else putchar(BEL);    /* otherwise just beep and */
  1779.         continue;        /* continue, don't put in buffer */
  1780.         }
  1781.         if (echof) cmdecho((char) c, 0); /* Echo what was typed. */
  1782.         } else {            /* This character was quoted. */
  1783.         int qf = 1;
  1784.         quote = 0;            /* Unset the quote flag. */
  1785.         /* Quote character at this level is only for SP, ?, and controls */
  1786.             /* If anything else was quoted, leave quote in, and let */
  1787.         /* the command-specific parsing routines handle it, e.g. \007 */
  1788.         if (c > 32 && c != '?' && c != RUB && chsrc != 0) {
  1789.         *bp++ = CMDQ;        /* Deposit \ if it came from tty */
  1790.         qf = 0;            /* and don't erase it from screen */
  1791.         }
  1792.         if (echof) cmdecho((char) c, qf); /* Now echo quoted character */
  1793.         debug(F000,"gtword quote",cmdbuf,c);
  1794.     }
  1795. #ifdef COMMENT
  1796.         if (echof) cmdecho((char) c,quote); /* Echo what was typed. */
  1797. #endif /* COMMENT */
  1798.         if (!comment) inword = 1;    /* Flag we're in a word. */
  1799.     if (quote) continue;        /* Don't deposit quote character. */
  1800.         if (c != NL) *bp++ = c;        /* Deposit command character. */
  1801.     }                                   /* End of big while */
  1802.     putchar(BEL);                       /* Get here if... */
  1803.     printf("?Command too long, maximum length: %d.\n",CMDBL);
  1804.     cmflgs = -2;
  1805.     return(-9);
  1806. }
  1807.  
  1808. /* Utility functions */
  1809.  
  1810. /* A D D B U F  -- Add the string pointed to by cp to the command buffer  */
  1811.  
  1812. static int
  1813. addbuf(cp) char *cp; {
  1814.     int len = 0;
  1815.     while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  1816.         *bp++ = *cp++;                  /* Copy and */
  1817.         len++;                          /* count the characters. */
  1818.     }   
  1819.     *bp++ = SP;                         /* Put a space at the end */
  1820.     *bp = NUL;                          /* Terminate with a null */
  1821.     np = bp;                            /* Update the next-field pointer */
  1822.     return(len);                        /* Return the length */
  1823. }
  1824.  
  1825. /*  S E T A T M  --  Deposit a token in the atom buffer.  */
  1826. /*  Break on space, newline, carriage return, or null. */
  1827. /*  Null-terminate the result. */
  1828. /*  If the source pointer is the atom buffer itself, do nothing. */
  1829. /*  Return length of token, and also set global "cc" to this length. */
  1830.  
  1831. static int
  1832. setatm(cp) char *cp; {
  1833.     char *ap, *xp;
  1834.  
  1835.     cc = 0;                /* Character counter */
  1836.     ap = atmbuf;            /* Address of atom buffer */
  1837.  
  1838.     if (cp == ap) {            /* In case source is atom buffer */
  1839.     xp = atybuf;            /* make a copy */
  1840.     strcpy(xp,ap);            /* so we can copy it back, edited. */
  1841.     cp = xp;
  1842.     }
  1843.     *ap = NUL;                /* Zero the atom buffer */
  1844.  
  1845.     while (*cp == SP) cp++;        /* Trim leading spaces */
  1846.     while ((*cp != SP) && (*cp != NL) && (*cp != NUL) && (*cp != CR)) {
  1847.         *ap++ = *cp++;            /* Copy up to SP, NL, CR, or end */
  1848.         cc++;                /* and count */
  1849.     }
  1850.     *ap = NUL;                /* Terminate the string. */
  1851.     return(cc);                         /* Return length. */
  1852. }
  1853.  
  1854. /*  R D I G I T S  -- Verify that all the characters in line ARE DIGITS  */
  1855.  
  1856. int
  1857. rdigits(s) char *s; {
  1858.     while (*s) {
  1859.         if (!isdigit(*s)) return(0);
  1860.         s++;
  1861.     }
  1862.     return(1);
  1863. }
  1864.  
  1865. /* These functions attempt to hide system dependencies from the mainline */
  1866. /* code in gtword().  Ultimately they should be moved to ck?tio.c, where */
  1867. /* ? = each and every system supported by C-Kermit. */
  1868.  
  1869. static int
  1870. cmdgetc() {                /* Get a character from the tty. */
  1871.     int c;
  1872.  
  1873. #ifdef datageneral
  1874.     {
  1875.     char ch;
  1876.     c = dgncinb(0,&ch,1);        /* -1 is EOF, -2 TO, 
  1877.                                          * -c is AOS/VS error */
  1878.     if (c == -2) {            /* timeout was enabled? */
  1879.         resto(channel(0));        /* reset timeouts */
  1880.         c = dgncinb(0,&ch,1);    /* retry this now! */
  1881.     }
  1882.     if (c < 0) return(-4);        /* EOF or some error */
  1883.     else c = (int) ch & 0177;    /* Get char without parity */
  1884. /*    echof = 1; */
  1885.     }
  1886. #else /* Not datageneral */
  1887. #ifdef GEMDOS
  1888.     c = isatty(0) ? coninc(0) : getchar();
  1889. #else
  1890. #ifdef OS2
  1891.     c = isatty(0) ? coninc(0) : getchar();
  1892.     if (c < 0) return(-4);
  1893. #else /* Not OS2 */
  1894.     c = getchar();            /* or from tty. */
  1895. #ifdef RTU
  1896.     if (rtu_bug) {
  1897.     c = getchar();            /* RTU doesn't discard the ^Z */
  1898.     rtu_bug = 0;
  1899.     }
  1900. #endif /* RTU */
  1901. #endif /* OS2 */
  1902. #endif /* GEMDOS */
  1903. #endif /* datageneral */
  1904.     return(c);                /* Return what we got */
  1905. }
  1906.  
  1907.  
  1908. #ifdef COMMENT
  1909. /*
  1910.   No more screen clearing.  If you wanna clear the screen, define a macro
  1911.   to do it, like "define cls write screen \27[;H\27[2J".
  1912. */
  1913. cmdclrscn() {                /* Clear the screen */
  1914.  
  1915. #ifdef aegis
  1916.     putchar(FF);
  1917. #else
  1918. #ifdef AMIGA
  1919.     putchar(FF);
  1920. #else
  1921. #ifdef OSK
  1922.     putchar(FF);
  1923. #else
  1924. #ifdef datageneral
  1925.     putchar(FF);
  1926. #else
  1927. #ifdef OS2
  1928.     zsystem("cls");
  1929. #else
  1930.     zsystem("clear");
  1931. #endif /* OS2 */
  1932. #endif /* datageneral */
  1933. #endif /* OSK */
  1934. #endif /* AMIGA */
  1935. #endif /* aegis */
  1936. }
  1937. #endif /* COMMENT */
  1938.  
  1939. static VOID                /* What to echo at end of command */
  1940. #ifdef CK_ANSIC
  1941. cmdnewl(char c)
  1942. #else
  1943. cmdnewl(c) char c;
  1944. #endif /* CK_ANSIC */
  1945. /* cmdnewl */ {
  1946.     putchar(c);                /* c is the terminating character */
  1947. #ifdef WINTCP
  1948.     if (c == CR) putchar(NL);
  1949. #endif /* WINTCP */
  1950. #ifdef OS2
  1951.     if (c == CR) putchar(NL);
  1952. #endif /* OS2 */
  1953. #ifdef aegis
  1954.     if (c == CR) putchar(NL);
  1955. #endif /* aegis */
  1956. #ifdef AMIGA
  1957.     if (c == CR) putchar(NL);
  1958. #endif /* AMIGA */
  1959. #ifdef datageneral
  1960.     if (c == CR) putchar(NL);
  1961. #endif /* datageneral */
  1962. #ifdef GEMDOS
  1963.     if (c == CR) putchar(NL);
  1964. #endif /* GEMDOS */
  1965. }
  1966.  
  1967. static VOID
  1968. cmdchardel() {                /* Erase a character from the screen */
  1969.     if (!dpx) return;
  1970. #ifdef datageneral
  1971.     /* DG '\b' is EM (^y or \031) */
  1972.     if (termtype == 1)
  1973.       /* Erase a character from non-DG screen, */
  1974.       dgncoub(1,"\010 \010",3);
  1975.     else
  1976. #endif
  1977.       printf("\b \b");
  1978. #ifdef GEMDOS
  1979.     fflush(stdout);
  1980. #endif /* GEMDOS */
  1981. }
  1982.  
  1983. static VOID
  1984. #ifdef CK_ANSIC
  1985. cmdecho(char c, int quote)
  1986. #else
  1987. cmdecho(c,quote) char c; int quote;
  1988. #endif /* CK_ANSIC */
  1989. { /* cmdecho */
  1990.     if (!dpx) return;
  1991.     /* Echo tty input character c */
  1992.     if (quote) {
  1993.     putchar(BS); putchar(SP); putchar(BS); 
  1994. #ifdef isprint
  1995.     putchar( isprint(c) ? c : '^' );
  1996. #else
  1997.     putchar((c >= SP && c < DEL) ? c : '^');
  1998. #endif /* isprint */
  1999.     } else putchar(c);
  2000. #ifdef OS2
  2001.     if (quote==1 && c==CR) putchar(NL);
  2002. #endif /* OS2 */
  2003. }
  2004.  
  2005. #endif /* NOICP */
  2006.  
  2007. #ifdef NOICP
  2008. #include "ckcdeb.h"
  2009. #include "ckucmd.h"
  2010. #include "ckcasc.h"
  2011. /*** #include <ctype.h> (ckcdeb.h already includes this) ***/
  2012. #endif /* NOICP */
  2013.  
  2014. /*  X X E S C  --  Interprets backslash codes  */
  2015. /*  Returns the int value of the backslash code if it is > -1 and < 256 */
  2016. /*  and updates the string pointer to first character after backslash code. */
  2017. /*  If the argument is invalid, leaves pointer unchanged and returns -1. */
  2018.  
  2019. int
  2020. xxesc(s) char **s; {            /* Expand backslash escapes */
  2021.     int x, y, brace, radix;        /* Returns the int value */
  2022.     char hd = '9';            /* Highest digit in radix */
  2023.     char *p;
  2024.  
  2025.     p = *s;                /* pointer to beginning */
  2026.     if (!p) return(-1);            /* watch out for null pointer */
  2027.     x = *p++;                /* character at beginning */
  2028.     if (x != CMDQ) return(-1);        /* make sure it's a backslash code */
  2029.  
  2030.     x = *p;                /* it is, get the next character */
  2031.     if (x == '{') {            /* bracketed quantity? */
  2032.     p++;                /* begin past bracket */
  2033.     x = *p;
  2034.     brace = 1;
  2035.     } else brace = 0;
  2036.     switch (x) {            /* Start interpreting */
  2037.       case 'd':                /* Decimal radix indicator */
  2038.       case 'D':
  2039.     p++;                /* Just point past it and fall thru */
  2040.       case '0':                /* Starts with digit */
  2041.       case '1':
  2042.       case '2':  case '3':  case '4':  case '5':
  2043.       case '6':  case '7':  case '8':  case '9':
  2044.     radix = 10;            /* Decimal */
  2045.     hd = '9';            /* highest valid digit */
  2046.     break;
  2047.       case 'o':                /* Starts with o or O */
  2048.       case 'O':
  2049.     radix = 8;            /* Octal */
  2050.     hd = '7';            /* highest valid digit */
  2051.     p++;                /* point past radix indicator */
  2052.     break;
  2053.       case 'x':                /* Starts with x or X */
  2054.       case 'X':
  2055.     radix = 16;            /* Hexadecimal */
  2056.     p++;                /* point past radix indicator */
  2057.     break;
  2058.       default:                /* All others */
  2059. #ifdef COMMENT
  2060.     *s = p+1;            /* Treat as quote of next char */
  2061.     return(*p);
  2062. #else
  2063.     return(-1);
  2064. #endif /* COMMENT */
  2065.     }
  2066.     /* For OS/2, there are "wide" characters required for the keyboard
  2067.      * binding, i.e \644 and similar codes larger than 255 (byte).
  2068.      * For this purpose, give up checking for < 256. If someone means
  2069.      * \266 should result in \26 followed by a "6" character, he should
  2070.      * always write \{26}6 anyway.  Now, return only the lower byte of
  2071.      * the result, i.e. 10, but eat up the whole \266 sequence and
  2072.      * put the wide result 266 into a global variable.  Yes, that's not
  2073.      * the most beautiful programming style but requires the least
  2074.      * amount of changes to other routines.
  2075.      */
  2076.     if (radix <= 10) {            /* Number in radix 8 or 10 */
  2077.     for ( x = y = 0;
  2078.            (*p) && (*p >= '0') && (*p <= hd)
  2079. #ifdef OS2
  2080.                    && (y < 4) && (x*radix < 768);
  2081.               /* the maximum needed value \767 is still only 3 digits long */
  2082.               /* while as octal it requires \1377, i.e. 4 digits */
  2083. #else
  2084.                    && (y < 3) && (x*radix < 256);
  2085. #endif /* OS2 */
  2086.           p++,y++) {
  2087.         x = x * radix + (int) *p - 48;
  2088.     }
  2089. #ifdef OS2
  2090.         wideresult = x;            /* Remember wide result */
  2091.         x &= 255;
  2092. #endif /* OS2 */
  2093.     if (y == 0 || x > 255) {    /* No valid digits? */
  2094.         *s = p;            /* point after it */
  2095.         return(-1);            /* return failure. */
  2096.     }
  2097.     } else if (radix == 16) {        /* Special case for hex */
  2098.     if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  2099.     if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  2100.     x = ((x << 4) & 0xF0) | (y & 0x0F);
  2101. #ifdef OS2
  2102.         wideresult = x;
  2103.         if ((y = unhex(*p)) >= 0) {
  2104.            p++;
  2105.        wideresult = ((x << 4) & 0xFF0) | (y & 0x0F);
  2106.            x = wideresult & 255;
  2107.         }
  2108. #endif /* OS2 */
  2109.     } else x = -1;
  2110.     if (brace && *p == '}' && x > -1)    /* Point past closing brace, if any */
  2111.       p++;
  2112.     *s = p;                /* Point to next char after sequence */
  2113.     return(x);                /* Return value of sequence */
  2114. }
  2115.  
  2116. int                    /* Convert hex string to int */
  2117. #ifdef CK_ANSIC
  2118. unhex(char x)
  2119. #else
  2120. unhex(x) char x;
  2121. #endif /* CK_ANSIC */
  2122. /* unhex */ {
  2123.  
  2124.     if (x >= '0' && x <= '9')        /* 0-9 is offset by hex 30 */
  2125.       return(x - 0x30);
  2126.     else if (x >= 'A' && x <= 'F')    /* A-F offset by hex 37 */
  2127.       return(x - 0x37);
  2128.     else if (x >= 'a' && x <= 'f')    /* a-f offset by hex 57 */
  2129.       return(x - 0x57);            /* (obviously ASCII dependent) */
  2130.     else return(-1);
  2131. }
  2132.  
  2133. /* See if argument string is numeric */
  2134. /* Returns 1 if OK, zero if not OK */
  2135. /* If OK, string should be acceptable to atoi() */
  2136. /* Allows leading space, sign */
  2137.  
  2138. int
  2139. chknum(s) char *s; {            /* Check Numeric String */
  2140.     int x = 0;                /* Flag for past leading space */
  2141.     int y = 0;                /* Flag for digit seen */
  2142.     char c;
  2143.     debug(F110,"chknum",s,0);
  2144.     while (c = *s++) {            /* For each character in the string */
  2145.     switch (c) {
  2146.       case SP:            /* Allow leading spaces */
  2147.       case HT:
  2148.         if (x == 0) continue;
  2149.         else return(0);
  2150.       case '+':            /* Allow leading sign */
  2151.       case '-':
  2152.         if (x == 0) x = 1;
  2153.         else return(0);
  2154.         break;
  2155.       default:            /* After that, only decimal digits */
  2156.         if (c >= '0' && c <= '9') {
  2157.         x = y = 1;
  2158.         continue;
  2159.         } else return(0);
  2160.     }
  2161.     }
  2162.     return(y);
  2163. }
  2164.  
  2165. /*  L O W E R  --  Lowercase a string  */
  2166.  
  2167. int
  2168. lower(s) char *s; {
  2169.     int n = 0;
  2170.     while (*s) {
  2171.         if (isupper(*s)) *s = tolower(*s);
  2172.         s++, n++;
  2173.     }
  2174.     return(n);
  2175. }
  2176.  
  2177. /*  L O O K U P  --  Lookup the string in the given array of strings  */
  2178.  
  2179. /*
  2180.  Call this way:  v = lookup(table,word,n,&x);
  2181.  
  2182.    table - a 'struct keytab' table.
  2183.    word  - the target string to look up in the table.
  2184.    n     - the number of elements in the table.
  2185.    x     - address of an integer for returning the table array index.
  2186.  
  2187.  The keyword table must be arranged in ascending alphabetical order, and
  2188.  all letters must be lowercase.
  2189.  
  2190.  Returns the keyword's associated value ( zero or greater ) if found,
  2191.  with the variable x set to the array index, or:
  2192.  
  2193.   -3 if nothing to look up (target was null),
  2194.   -2 if ambiguous,
  2195.   -1 if not found.
  2196.  
  2197.  A match is successful if the target matches a keyword exactly, or if
  2198.  the target is a prefix of exactly one keyword.  It is ambiguous if the
  2199.  target matches two or more keywords from the table.
  2200. */
  2201.  
  2202. int
  2203. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  2204.  
  2205.     int i, v, cmdlen;
  2206.  
  2207. /* Lowercase & get length of target, if it's null return code -3. */
  2208.  
  2209.     if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3);
  2210.  
  2211. /* Not null, look it up */
  2212.  
  2213.     for (i = 0; i < n-1; i++) {
  2214.         if (!strcmp(table[i].kwd,cmd) ||
  2215.            ((v = !strncmp(table[i].kwd,cmd,cmdlen)) &&
  2216.              strncmp(table[i+1].kwd,cmd,cmdlen))) {
  2217.                 *x = i;
  2218.                 return(table[i].kwval);
  2219.              }
  2220.         if (v) return(-2);
  2221.     }   
  2222.  
  2223. /* Last (or only) element */
  2224.  
  2225.     if (!strncmp(table[n-1].kwd,cmd,cmdlen)) {
  2226.         *x = n-1;
  2227.         return(table[n-1].kwval);
  2228.     } else return(-1);
  2229. }
  2230.